//
//  ViewController.m
//  JKTZNSOperationQuene
//
//  Created by 王冲 on 2019/1/25.
//  Copyright © 2019年 JK科技有限公司. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@property(nonatomic,strong) NSOperationQueue *opQueue;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIButton *buttton1 = [[UIButton alloc]initWithFrame:CGRectMake(20, 150, 100, 40)];
    [buttton1 setTitle:@"暂停/继续" forState:UIControlStateNormal];
    [buttton1 setTitleColor:[UIColor brownColor] forState:UIControlStateNormal];
    [buttton1 setBackgroundColor:[UIColor yellowColor]];
    [buttton1 addTarget:self action:@selector(startLoadTask) forControlEvents:UIControlEventTouchUpInside];
    buttton1.titleLabel.font = [UIFont systemFontOfSize:18.f];
    [self.view addSubview:buttton1];
    
    UIButton *buttton2 = [[UIButton alloc]initWithFrame:CGRectMake(20, CGRectGetMaxY(buttton1.frame)+50, 200, 40)];
    [buttton2 setTitle:@"取消所有的操作" forState:UIControlStateNormal];
    [buttton2 setTitleColor:[UIColor brownColor] forState:UIControlStateNormal];
    [buttton2 setBackgroundColor:[UIColor yellowColor]];
    [buttton2 addTarget:self action:@selector(cancleTask) forControlEvents:UIControlEventTouchUpInside];
    buttton2.titleLabel.font = [UIFont systemFontOfSize:18.f];
    [self.view addSubview:buttton2];
    
}

-(NSOperationQueue *)opQueue{
    
    if (!_opQueue) {
        
        _opQueue = [[NSOperationQueue alloc]init];
    }
    
    return _opQueue;
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    [self dependecy];
}

-(void)demo1{
   
    /*
    从iOS 8.0 开始，无论是GCD 还是NSOperation 都会开启很多线程
    在 iOS 7.0以前GCD ,一般开5-6；
    目前线程多说明：
    1.底层的线程池更大了，能够拿到更多的线程资源了
    2.多线程同时并发的现场数，要求更高了。
     */
    
    // 设置最大线程数
    self.opQueue.maxConcurrentOperationCount = 2;
    
    for (int i = 0; i<20; ++i) {
        
        [self.opQueue addOperationWithBlock:^{
            
            [NSThread sleepForTimeInterval:1.0];
            
            NSLog(@"%@------%d",[NSThread currentThread],i);
        }];
        
    }
    
    NSLog(@"全部添加进去了");
    
}

/**
   当挂起队列的时候，正在执行的操作是不是被挂起的！
   suspended：决定队列的暂停与继续
   operationCount：队列中的操作数
 */
-(void)startLoadTask{
    
    // 判断队列是否挂起
    if (self.opQueue.isSuspended) {
        
        NSLog(@"继续 队列的操作数=%tu",self.opQueue.operationCount);
        self.opQueue.suspended = NO;
    }else{
        
        NSLog(@"暂停");
        self.opQueue.suspended = YES;
    }
}

#pragma mark 取消所有的操作
-(void)cancleTask{
    
    [self.opQueue cancelAllOperations];
}

#pragma mark 依赖的测试
-(void)dependecy{
    
    /**
       下载、解压、通知用户
     */
    
    // 1.下载
    NSBlockOperation *op1 = [NSBlockOperation blockOperationWithBlock:^{
        
        [NSThread sleepForTimeInterval:3.f];
        NSLog(@"下载---%@",[NSThread currentThread]);
        
    }];
    
    // 2.解压
    NSBlockOperation *op2 = [NSBlockOperation blockOperationWithBlock:^{
        
        [NSThread sleepForTimeInterval:5.f];
        NSLog(@"解压---%@",[NSThread currentThread]);
        
    }];
    
    // 3.通知用户
    NSBlockOperation *op3 = [NSBlockOperation blockOperationWithBlock:^{
        
        NSLog(@"通知用户---%@",[NSThread currentThread]);
        
    }];
    
    // NSOperation 提供依赖关系
    [op2 addDependency:op1];
    [op3 addDependency:op2];
    
    // YES：等队列完成后再执行后面的，NO不需要等队列完成再去执行下面的操作
    [self.opQueue addOperations:@[op1,op2] waitUntilFinished:NO];
    
    // 通知用户要在主线程
    [[NSOperationQueue mainQueue] addOperation:op3];
    
    NSLog(@"来这里了 --- %@",[NSThread currentThread]);
    
}

@end
